/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var DataSource = new Class({
    
    items        : new Array(),
    revision     : 0,
    pointer      : 0,
    slidingDoors : null,  
    
    Implements: [Options,Events],
    
    options: {
        id      : '',
        content : ''
    },
    
    initialize: function(options){
        this.setOptions(options);
    },
    
    addItem: function(item){        
        this.items[this.items.length] = item;
    },

    getId: function(){
        return this.options.id;
    },
    
    getCurrentItem: function(){
        return this.items[this.pointer];
    },
    
    getNextItem: function(){
        return this.items[(this.pointer+1)%this.items.length];
    },
    
    setSlidingDoors: function(sd){
        this.slidingDoors = sd;
    },
    
    shift: function(){
      this.pointer = ++this.pointer%this.items.length;
      //console.log(this.pointer);
    },
    
    updateRevision: function(){
        this.revision++;
    },
    
    startLoop: function(el){       
        
        var loop = function(item){
            item.removeEvents();
            //kontrolni revize
            item.setRevision(this.revision);
            item.addEvent('end',function(item,revision){
                item.removeEvents('end');
                //kontrola dojezdu z predchoziho pruchodu
                //console.log('source revision:'+this.revision+',item revision:'+revision);
                //console.log('item end:'+item.options.url+' from el:'+item.options.el.getParent().getParent().get('id'));
                if(this.revision != revision){
                    console.error('Wrong revision');
                    return;
                }    
                //
                //console.log(item.options.url);
                this.shift();
                //console.log(this.pointer);
                if(this.pointer == 0){
                    console.log('Source timelineend. Trigger:'+item.options.url);
                    this.fireEvent('timelineEnd',this);
                }else{
                    loop(this.getCurrentItem());
                }                
            }.bind(this));
            if(item.isValid()){
                if(this.slidingDoors.getCurrent().getParent('.preloading')){
                    this.slidingDoors.getCurrent().getParent('.preloading').removeClass('preloading');
                }
                item.show();
                this.slidingDoors.addEvent('end', function(slidingDoors){ 
                    //slidingDoors.removeEvents('end');
                    this.slidingDoors.removeEvents();
                    var nextItem = this.getNextItem();
                    //console.log()
                    if(nextItem != item && ((this.pointer+1)%this.items.length) != 0){ 
                        if(nextItem.isValid())
                            nextItem.load(this.slidingDoors.getCurrent());
                    }
                }.bind(this));
                this.slidingDoors.slide(); 
            }else{
                item.fireEvent('end', [item,item.getRevision()]);
            }
        }.bind(this);
        //set variables
        this.pointer  = 0; 
        var cntValid  = 0;
        var firstItem = null;
        //test empty loop and find first item
        Array.each(this.items, function(item,key){
            if(item.isValid()){                
                cntValid++;
                if(firstItem == null){
                    firstItem = item;
                }
            }
        });
        if(cntValid == 0){
            this.fireEvent('timelineEnd', this, 1000);
        }else{           
            firstItem.load(this.slidingDoors.getCurrent()); 
            //start loop
            loop(firstItem); 
        }
    },
    
    stopLoop: function(){
 
    }
    
});


